﻿using HamcoWcs.Entitys.Devices;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DeviceMsgs;
using HamcoWcs.Models.NodeInfos;
using System;
using System.Drawing;

namespace HamcoWcs.Models.Devices
{
    public class PconDto
    {
        public string PlcName { get; set; }

        // ppk add 对应一条线
        public NodeInfoDto Machine { get; set; }
        // ppk add
        public string MachineName { get; set; }
        // ppk add
        public string PortName { get; set; }
        public EnumPconType PconType { get; set; }
        public string PortState { get; set; } = "";
        public int SendStartPoint { get; set; }
        public int RecvStartPoint { get; set; }


        public int PconName { get; set; }
        public int PconMode { get; set; }
        public int ErrorCode { get; set; }
        public int IsGoods { get; set; }
        public int CheckRes { get; set; }
        public int CheckResult { get; set; }
        public int Weight { get; set; }
        public int DestLoc { get; set; }
        public ushort Speed { get; set; }
        public string UnitId { get; set; }
        public int WorkType { get; set; }
        public int Request { get; set; }


        public ushort Size { get; set; }
        public UInt32 DevLoc { get; set; }
        public uint Input { get; set; }
        public uint Output { get; set; }

        public string DeviceName => $"{MachineName}-{PortName}";

        public string  PalletType { get; set; }
        public bool IsReservation { get; set; }
        public string DisableDesc { get; set; }

        public UInt32 RgvPosition { get; set; } = 0;

        /// <summary>
        /// 2023/07/31 Add 异常计时用，每1分钟上报
        /// </summary>
        public int AlarmTick { get; set; } = 0;

        private EnumMachineState _machineState = EnumMachineState.NONE;

        public EnumMachineState MachineState
        {
            get => _machineState;
            set
            {
                if (_machineState != value)
                    IsNeedReport = true;
                if (_machineState == EnumMachineState.DOWN && value == EnumMachineState.IDLE)
                {
                    _machineState = EnumMachineState.RUN;
                    return;
                }
                _machineState = value;
            }
        }
        public bool IsNeedReport { get; set; } = false;
        public void Update(PconRecvMsg pconRecvMsg)
        {
            PconMode = pconRecvMsg.PconMode;
            ErrorCode = pconRecvMsg.ErrorCode;
            IsGoods = pconRecvMsg.IsGoods;
            CheckRes = pconRecvMsg.CheckRes;
            CheckResult = pconRecvMsg.CheckResult;
            Weight = pconRecvMsg.Weight;
            Speed = pconRecvMsg.Speed;
            UnitId = pconRecvMsg.UnitId;
            WorkType = pconRecvMsg.WorkType;
            Request = pconRecvMsg.Request;
            Size = pconRecvMsg.Size;
            DevLoc = pconRecvMsg.DevLoc;
            //Input = Convert.ToString(pconRecvMsg.Input, 2).PadLeft(32, '0');
            //Output = Convert.ToString(pconRecvMsg.Output, 2).PadLeft(32, '0');
            Input = pconRecvMsg.Input;
            Output = pconRecvMsg.Output;
            DestLoc = pconRecvMsg.DestLoc;

            if (pconRecvMsg.PconMode != 5 && DisableDesc=="启用失败")
            {
                DisableDesc = "";
            }

            if (pconRecvMsg.PconMode == 5 && DisableDesc == "设宕失败")
            {
                DisableDesc = "";
            }

            //PconMode = pconRecvMsg.PconMode;
            //ErrorCode = pconRecvMsg.ErrorCode;
            //IsGoods = pconRecvMsg.IsGoods;
            //CheckRes = pconRecvMsg.CheckRes;
            //CheckResult = pconRecvMsg.CheckResult;
            //Weight = pconRecvMsg.Weight;
            //DestLoc = pconRecvMsg.DestLoc;
            //UnitId = pconRecvMsg.UnitId;
            //WorkType = pconRecvMsg.WorkType;
            //Request = pconRecvMsg.Request;
            //Speed = pconRecvMsg.Speed;
            //Machine.UpdatePcon(this);
        }
    }
}
